﻿using System.Collections.Generic;
using System.ComponentModel;
using VNet.VisiSuite.Common.Security;
using VNet.VisiSuite.Service.Contact.Message.Base;
using VNet.VisiSuite.Service.Contact.Message.Item;
using VNet.VisiSuite.Service.Proxy;

namespace VNet.VisiSuite.VisiAdmin.Providers
{
    public class ProductProviders
    {
        public static void GetProducts(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;

            if (worker != null && worker.CancellationPending == false)
            {
                using (var client = new VNetVisiSuiteContractClient())
                {
                    var request = new GetDataRequest
                    {
                        Identity = UserTransportToken.UserIdentity,
                    };
                    e.Result = client.GetProducts(request);
                }
            }
        }

        public static void ImportProducts(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;

            if (worker != null && worker.CancellationPending == false)
            {
                var productItems = e.Argument as List<ProductItem>;

                using (var client = new VNetVisiSuiteContractClient())
                {
                    var request = new SaveDataRequest<IList<ProductItem>>
                    {
                        Identity = UserTransportToken.UserIdentity,
                        Data = productItems,
                    };
                    e.Result = client.ImportProducts(request);
                }
            }
        }

        public static void DeleteProduct(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;
            if (worker != null && worker.CancellationPending == false)
            {
                var productId =(int)e.Argument;

                using (var client = new VNetVisiSuiteContractClient())
                {
                    var request = new DeleteDataRequest
                    {
                        Identity = UserTransportToken.UserIdentity,
                        DataId = productId,
                    };
                    e.Result = client.DeleteProduct(request);
                }
            }
        }

        public static void InsertProduct(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;

            if (worker != null && worker.CancellationPending == false)
            {
                var productItem = e.Argument as ProductItem;

                using (var client = new VNetVisiSuiteContractClient())
                {
                    var request = new SaveDataRequest<ProductItem>
                    {
                        Identity = UserTransportToken.UserIdentity,
                        Data = productItem,
                    };
                    e.Result = client.InsertProduct(request);
                }
            }
        }

        public static void UpdateProduct(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;

            if (worker != null && worker.CancellationPending == false)
            {
                var productItem = e.Argument as ProductItem;

                using (var client = new VNetVisiSuiteContractClient())
                {
                    var request = new SaveDataRequest<ProductItem>
                    {
                        Identity = UserTransportToken.UserIdentity,
                        Data = productItem,
                    };
                    e.Result = client.UpdateProduct(request);
                }
            }
        }
    }
}
